package cn.aitrox.ry.service.anno.controller;

import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.bean.RespResult;
import cn.aitrox.ry.service.anno.api.TaskApi;
import cn.aitrox.ry.service.anno.dto.task.TaskDto;
import cn.aitrox.ry.service.anno.dto.task.TaskDetailDto;
import cn.aitrox.ry.service.anno.dto.task.in.AnnoSeriesDto;
import cn.aitrox.ry.service.anno.dto.task.in.TaskCreateInDto;
import cn.aitrox.ry.service.anno.dto.task.in.TaskEditInDto;
import cn.aitrox.ry.service.anno.dto.task.in.TaskNameExistedInDto;
import cn.aitrox.ry.service.anno.dto.task.in.TaskQueryInDto;
import cn.aitrox.ry.service.anno.dto.task.in.TaskSearchInDto;
import cn.aitrox.ry.service.anno.service.TaskService;
import cn.aitrox.ry.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class TaskController implements TaskApi {

    @Autowired
    private TaskService taskService;

    @PostMapping(value = "/anno/task/search")
    @Override
    public CommonPageInfo<TaskDto> search(@Validated @RequestBody TaskSearchInDto input) {
        if (null != input.getBeginDate()) {
            input.setBeginDate(this.dateConvert(input.getBeginDate()));
        }
        if (null != input.getEndDate()) {
            input.setEndDate(this.dateConvert(input.getEndDate()));
        }

        return taskService.search(input);
    }

    @PostMapping(value = "/anno/task/status/edit")
    @Override
    public RespResult statusEdit(@RequestParam("id") Integer id, @RequestParam("status") Integer status) {
        return taskService.statusEdit(id, status);
    }

    @PostMapping(value = "/anno/task")
    @Override
    public RespResult<TaskDto> create(@Validated @RequestBody TaskCreateInDto input) {
        return taskService.create(input);
    }

    @PutMapping(value = "/anno/task")
    @Override
    public RespResult edit(@Validated @RequestBody TaskEditInDto input) {
        return taskService.edit(input);
    }

    @GetMapping(value = "/anno/task/{id}")
    @Override
    public TaskDetailDto read(@PathVariable("id") Integer id) {
        return taskService.read(id);
    }

    @PostMapping(value = "/anno/task/copy")
    @Override
    public RespResult copy(@RequestParam("taskId") Integer taskId) {
        return taskService.copy(taskId);
    }

    @PostMapping(value = "/anno/task/query")
    @Override
    public List<TaskDto> query(@RequestBody TaskQueryInDto input) {
        return taskService.query(input.getIds());
    }

    @PostMapping(value = "/anno/task/name/existed")
    @Override
    public Boolean taskNameExisted(@RequestBody TaskNameExistedInDto input) {
        if (StringUtils.isEmpty(input.getName())) {
            return false;
        }
        return taskService.taskNameExisted(input.getId(), input.getName());
    }

    @PostMapping(value = "/anno/task/series")
    @Override
    public List<AnnoSeriesDto> taskSeries(@PathVariable("id") Integer id) {
        return taskService.taskSeries(id);
    }

    private Date dateConvert(Date date) {
        if (null == date) {
            return null;
        }
        return DateUtil.parseTime(DateUtil.formatDate(date, DateUtil.DEFAULT_DATE_FORMAT), DateUtil.DEFAULT_DATE_FORMAT);
    }
}
